package com.jyh.util;

   
	 /**     
     * @Description 把数据返回 List<Map<String, Object>> 结果集，封装成对象结果集
     * @author tank       
     * @created 2017年5月12日 上午10:47:37        
     */
    
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

    /**           
 * Description: 把数据返回 List<Map<String, Object>> 结果集，封装成对象结果集
 * @author tank       
 * @created 2017年5月12日 上午10:47:37    
 */

public class VOUtils {

		public static <T> List<T> getT(List<Map<String, Object>> listMap, Class<T> cls) {
			  List<T> lists = new ArrayList<>();
			  for (Map<String, Object> map : listMap) {
				   try {
				    lists.add(getT(map, cls));
				   } catch (InstantiationException e) {
				    e.printStackTrace();
				   } catch (IllegalAccessException e) {
				    e.printStackTrace();
				   } catch (Exception e) {
				    e.printStackTrace();
				   }
			  }
			  return lists;
		 }

		public static <T> T getT(Map<String, Object> map, Class<T> cls)
		   throws InstantiationException, IllegalAccessException, ClassNotFoundException {
			  T t2 = (T) cls.newInstance();
			  Field[] fields = t2.getClass().getDeclaredFields();
			  for (Field field : fields) {
				  if( null != map.get(field.getName())){
					   field.setAccessible(true);
					   try{
						   field.set(t2, map.get(field.getName()));
					   }catch(Exception e){
						   try {
							field.set(t2,getObj(field.getType(),map.get(field.getName()) + ""));
						} catch (IllegalArgumentException e1) {
							e1.printStackTrace();
						} catch (ParseException e1) {
							e1.printStackTrace();
						}
					   }
				   }
			  }
			  return t2;
		 }
		
		public static <T> T getObj(Map<String,String> map,Class<T> cls){
			try{
				 T t2 = (T) cls.newInstance();
				  Field[] fields = t2.getClass().getDeclaredFields();
				  for (Field field : fields) {
					  if( null != map.get(field.getName())){
						   field.setAccessible(true);
						   Class<?> typeClass = field.getType();
						   field.set(t2, getObj(typeClass,map.get(field.getName())));
					   }
				  }
				  return t2;
			}catch(Exception e){
				e.printStackTrace();
			}
			return null;
		}
		private static Object getObj(Class<?> typeCls,String val) throws ParseException{
			if(typeCls == Integer.class || typeCls.getName().contains("int")){
				return Integer.parseInt(val);
			}else if(typeCls == Double.class || typeCls.getName().contains("double")){
				return Double.parseDouble(val);
			}else if(typeCls == Short.class || typeCls.getName().contains("short")){
				return Short.parseShort(val);
			}else if(typeCls == Float.class || typeCls.getName().contains("float")){
				return Float.parseFloat(val);
			}else if(typeCls == Date.class){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date= sdf.parse(val);
				return date;
			}else if(typeCls == BigDecimal.class){
				return new BigDecimal(val);
			}else if(typeCls == Timestamp.class){
				return Timestamp.valueOf(val);
			}
			return val;
		}
//		public static void main(String[] args) {
//			Map<String, String> paramsMap = new HashMap<String, String>();
//			paramsMap.put("a", "1");
//			paramsMap.put("b", "1.0");
//			paramsMap.put("c", "1f");
//			paramsMap.put("d", "2111-12-01 12:00:03");
//			paramsMap.put("e", "222.0");
//			paramsMap.put("f", "2111-12-01 12:00:03");
//			paramsMap.put("g", "sfsdf");
//			paramsMap.put("h", "1");
//			paramsMap.put("i", "1.0");
//			paramsMap.put("j", "1f");
//			A a = getObj(paramsMap,A.class);
//			a.a = a.a +2;
//			System.out.println(a);
//			Date date = new Date();
//			Class<?> typeCls = date.getClass();
//			System.out.println(typeCls == Date.class);
//		}
}
class A{
	int a;
	double b;
	float c;
	short aa;
	char cc;
	Date d;
	BigDecimal e;
	Timestamp f;
	Float j;
}
